package light.head.controller.sys;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import light.head.constant.Parm;
import light.head.constant.Sys;
import light.head.constant.Url;
import light.head.dao.sys.FileDAO;
import light.head.enm.RESULT;
import light.head.framework.base.SupporterController;
import light.head.framework.entity.PageBean;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping(Url.C_BROWSE)
public class BrowseController extends SupporterController {
	
	private FileDAO fileDAO;

	@RequestMapping(Url.FILE)
	public ModelAndView file(HttpServletRequest request, HttpServletResponse response) {
		request.setAttribute("callback", request.getParameter("CKEditorFuncNum"));
		PageBean pb = new PageBean(request.getParameter(Sys.PAGE), 50);
		Map<String, String> map = new HashMap<String, String>();
		
		
		String time1 = request.getParameter(Parm.TIME1);
		String time2 = request.getParameter(Parm.TIME2);
		String name = request.getParameter(Parm.NAME);
		String type = request.getParameter(Parm.TYPE);
		
		map.put(Parm.TIME1, time1);
		map.put(Parm.TIME2, time2);
		map.put(Parm.NAME, name);
		map.put(Parm.TYPE, type);
		
		if (fileDAO.findByMap(cu_organCode(request), map, pb) == RESULT.SUCCESS)
			request.setAttribute(Parm.PAGE_BEAN, pb);
		
		request.setAttribute(Parm.TIME1, time1);
		request.setAttribute(Parm.TIME2, time2);
		request.setAttribute(Parm.NAME, name);
		request.setAttribute(Parm.TYPE, type);
		
		request.setAttribute(Parm.CKEditor, request.getParameter(Parm.CKEditor));
		request.setAttribute(Parm.langCode, request.getParameter(Parm.langCode));
		request.setAttribute(Parm.CKEditorFuncNum, request.getParameter(Parm.CKEditorFuncNum));
		
		return new ModelAndView(Url.R_FILE);
	}
	
	@RequestMapping(Url.IMAGE)
	public ModelAndView image(HttpServletRequest request, HttpServletResponse response) {
		request.setAttribute("callback", request.getParameter("CKEditorFuncNum"));
		PageBean pb = new PageBean(request.getParameter(Sys.PAGE), 50);
		Map<String, String> map = new HashMap<String, String>();
		
		String time1 = request.getParameter(Parm.TIME1);
		String time2 = request.getParameter(Parm.TIME2);
		String name = request.getParameter(Parm.NAME);
		String cu = request.getParameter(Parm.CU);
		
		map.put(Parm.TIME1, time1);
		map.put(Parm.TIME2, time2);
		map.put(Parm.NAME, name);
		map.put(Parm.CU, cu);
		map.put(Parm.TYPE, Sys.S_TWO);
		
		if (fileDAO.findByMap(cu_organCode(request), map, pb) == RESULT.SUCCESS)
			request.setAttribute(Parm.PAGE_BEAN, pb);
		
		request.setAttribute(Parm.TIME1, time1);
		request.setAttribute(Parm.TIME2, time2);
		request.setAttribute(Parm.NAME, name);
		request.setAttribute(Parm.CU, cu);
		
		request.setAttribute(Parm.CKEditor, request.getParameter(Parm.CKEditor));
		request.setAttribute(Parm.langCode, request.getParameter(Parm.langCode));
		request.setAttribute(Parm.CKEditorFuncNum, request.getParameter(Parm.CKEditorFuncNum));
		
		return new ModelAndView(Url.R_IMAGE);
	}

	@Autowired
	public void setFileDAO(FileDAO fileDAO) {
		this.fileDAO = fileDAO;
	}
}
